package indi.qing.controller;

import com.auth0.jwt.JWT;
import indi.qing.VO.ProcessVO;
import indi.qing.VO.RelationVO;
import indi.qing.common.Result;
import indi.qing.entity.Relation;
import indi.qing.service.RelationService;
import indi.qing.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 熊卿智
 * @since 2022-01-17
 */
@RestController
public class RelationController {

    @Autowired
    private UserService userService;

    @Autowired
    private RelationService relationService;

    @PostMapping("/user/addFriend")
    public Result addFriend(HttpServletRequest request,@RequestBody Relation relation){
        String token = request.getHeader("token");
        Long user1Id = Long.valueOf(JWT.decode(token).getClaim("userId").asString());
        relation.setUser1Id(user1Id);
        try {
            relationService.addFriend(relation);
            return Result.ok("申请成功");
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/user/getAllAdd")
    public Result getAllAdd(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = Long.valueOf(JWT.decode(token).getClaim("userId").asString());
        try {
            List<RelationVO> list = relationService.getAllAdd(userId);
            return Result.ok(list);
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }


    @PostMapping("/user/processAdd")
    public Result processAdd(@RequestBody ProcessVO processVO){
        try {
            relationService.processAdd(processVO);
            return Result.ok("操作成功");
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/user/getFriend")
    public Result getFriend(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = Long.valueOf(JWT.decode(token).getClaim("userId").asString());
        try {
            List<Relation> relationList = relationService.getFriend(userId);
            return Result.ok(relationList);
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/user/deleteFriend/{userId}")
    public Result deleteFriend(HttpServletRequest request, @PathVariable Long userId){
        String token = request.getHeader("token");
        Long user1Id = Long.valueOf(JWT.decode(token).getClaim("userId").asString());
        try {
            relationService.deleteFriend(user1Id,userId);
            return Result.ok("删除成功");
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }

}

